<?php

class PublicModel extends Model
{

    public function auth($datas)
    {
        $username = I('post.username', '', 'htmlspecialchars');
        $password = I('post.password', '', 'htmlspecialchars');
        $verify = I('post.validcode', '', 'htmlspecialchars');
        $type = I("post.type", 0); //1为普通员工


        if (empty($username))
        {
            redirect('Index', 2, '帐号错误！');
        } elseif (empty($password))
        {
            redirect('Index', 2, '密码必须！');
        }/* elseif ('' === $verify)
        {
            redirect('Index', 2, '验证码必须！');
        }
        if ($_SESSION['verify'] != md5($verify))
        {
            redirect('Index', 2, '验证码错误！');
        }*/


        $employee = M("employee");
        $map["username"] = $username;
        $map["password"] = md5($password);

        $data = $employee->where($map)->find();
        if (!$data) redirect('Index', 2, '用户名密码错误...');
        $active = $data["active"];
        if (!$active)
        {
            echo("你被锁定,不能登录;");
            exit();
        }


        session('username', $username);


        session('realname', $data["name"]);
        session("userid", $data["id"]);
        session("roleid", $data["role"]);


        $role = M("role");
        $roleinfo = $role->find($data["role"]);


        $tasktype = M("tasktype");
        $roletasktype = "";

        //   $map['path'] =array('like',"%,".$roleinfo["tasktype"].",%");

        $alltasktype = $roleinfo["tasktype"];
        $typearray = explode(",", $alltasktype);

        foreach ($typearray as $vt)
        {
            if (empty($vt)) continue;
            $map['path'] = array('like', "%" . $vt . ",%");
            $roletasktypearray = $tasktype->field("id")->order('id asc')->where($map)->select();

            foreach ($roletasktypearray as $k => $v)
                $roletasktype .= $v["id"] . ",";
        }

        $roletasktype = substr($roletasktype, 0, -1);
        session("roletasktype", $roletasktype);
        session("admin", $data["isadmin"]);
        session("isleader", $data["isleader"]);

        $loginMarked = C("TOKEN");
        $loginMarked = md5($loginMarked['admin_marked']);


        $shell = $data['username'] . md5($password . C('AUTH_CODE'));
        $_SESSION[$loginMarked] = "$shell";
        $shell .= "_" . time();
        setcookie($loginMarked, "$shell", 0, "/");
        $_SESSION['my_info'] = $data;

        $newdata["id"] = $data["id"];
        $newdata["lastlogin"] = date("Y-m-d H:i:s");
        $newdata["logtime"] = $data["logtime"] + 1;
        $newdata["logIP"] = get_client_ip();

        $employee->save($newdata);
        return array('status' => 1, 'info' => "登录成功", 'url' => "Index/index");
    }

    public function findPwd()
    {
        $datas = $_POST;
        $M = M("Admin");
        if ($_SESSION['verify'] != md5($_POST['verify_code']))
        {
            die(json_encode(array('status' => 0, 'info' => "验证码错误啦，再输入吧")));
        }
//        $this->check_verify_code();
        if (trim($datas['pwd']) == '')
        {
            return array('status' => 0, 'info' => "密码不能为空");
        }
        if (trim($datas['pwd']) != trim($datas['pwd1']))
        {
            return array('status' => 0, 'info' => "两次密码不一致");
        }
        $data['aid'] = $_SESSION['aid'];
        $data['pwd'] = md5(C("AUTH_CODE") . md5($datas['pwd']));
        $data['find_code'] = NULL;
        if ($M->save($data))
        {
            return array('status' => 1, 'info' => "你的密码已经成功重置", 'url' => U('Access/index'));
        } else
        {
            return array('status' => 0, 'info' => "密码重置失败");
        }
    }

}

?>
